<style type="text/css">
    .demo-upload-list {
        display: inline-block;
        width: 60px;
        height: 60px;
        text-align: center;
        line-height: 60px;
        border: 1px solid transparent;
        border-radius: 4px;
        overflow: hidden;
        background: #fff;
        position: relative;
        box-shadow: 0 1px 1px rgba(0, 0, 0, .2);
        margin-right: 4px;
    }

    .demo-upload-list img {
        width: 100%;
        height: 100%;
    }

    .demo-upload-list-cover {
        display: none;
        position: absolute;
        top: 0;
        bottom: 0;
        left: 0;
        right: 0;
        background: rgba(0, 0, 0, .6);
    }

    .demo-upload-list:hover .demo-upload-list-cover {
        display: block;
    }

    .demo-upload-list-cover i {
        color: #fff;
        font-size: 20px;
        cursor: pointer;
        margin: 0 2px;
    }
</style>
<template>
    <Form-item :label="upImageData.title" prop="image">
        <div class="demo-upload-list" v-for="item in imageList">
            <template v-if="item.status === 'finished'">
                <img :src="apiUtilBase + item.response.Msg">
                <div class="demo-upload-list-cover">
                    <Icon type="ios-eye-outline" @click="handleView(apiUtilBase + item.response.Msg)"></Icon>
                    <Icon type="ios-trash-outline" @click="handleRemove(item)"></Icon>
                </div>
            </template>
            <template v-else>
                <Progress v-if="item.showProgress" :percent="item.percentage" hide-info></Progress>
            </template>
        </div>
        <Upload ref="upload"
                :show-upload-list="false"
                :default-file-list="defaultList"
                :on-success="handleSuccess"
                :format="['jpg','jpeg','png']"
                :max-size="2048"
                :name="'uploadFile'"
                :on-format-error="handleFormatError"
                :on-exceeded-size="handleMaxSize"
                :before-upload="handleBeforeUpload"
                multiplelayout-content
                type="drag"
                :action="upLoadFile"
                style="display: inline-block;width:58px;">
            <div style="width: 58px;height:58px;line-height: 58px;">
                <Icon type="camera" size="20"></Icon>
            </div>
        </Upload>
        <Modal title="查看图片" v-model="visible">
            <img :src="seeImg" v-if="visible" style="width: 100%">
        </Modal>
    </Form-item>
</template>
<script>


    import apiUtil from '../assets/utils/api_util'

    export default {
        data() {
            return {
                visible: false,
                imageList: '',
                upLoadFile: apiUtil.urls.UploadFile,
                apiUtilBase: apiUtil.urls.baseUrl,
                defaultList: [],
                imagePost: [],
            }
        },
        props: {
            upImageData: {},
            imgItem: "",
        },

        created() {
        },
        mounted() {
            if (this.upImageData.edit == 1) {
                if (this.upImageData.img) {
                    this.$refs.upload.fileList.push(this.upImageData.img);
                }

            }
            this.uploadList();
        },
        methods: {
            uploadList() {
                let that = this;
                this.imageList = that.$refs.upload ? that.$refs.upload.fileList : [];
                // console.log('this.imageList~~:', this.imageList);
            },
            //预览上传图片
            handleView(url) {
                this.seeImg = url;
                this.visible = true;
                console.log('预览照片')
            },
            //删除图片
            handleRemove(file) {
                // 从 upload 实例删除数据
                let that = this;

                that.imagePost = [];

                const fileList = this.$refs.upload.fileList;
                this.$refs.upload.fileList.splice(fileList.indexOf(file), 1);
                this.$refs.upload.fileList.forEach(function (item, i) {
                    that.imagePost.push(that.apiUtilBase + item.response.Msg);
                });
            },
            //上传成功
            handleSuccess(res, file) {
                let that = this;
                file.url = file.response.data;
                that.imagePost = [];

                console.log('this.$refs.upload:', this.$refs.upload.fileList);
                this.$refs.upload.fileList.forEach(function (item, i) {
                    let imgItem = {
                        "img": item.response.Msg
                    }
                    that.imagePost.push(imgItem);
                });

                this.$emit("get-postImg-data", that.imagePost);
                this.uploadList();


            },
            //上传成功
            handleFormatError(file) {
                this.$Notice.warning({
                    title: '文件格式不正确',
                    desc: '文件 ' + file.name + ' 格式不正确，请上传 jpg 或 png 格式的图片。'
                });
            },
            handleMaxSize(file) {
                this.$Notice.warning({
                    title: '超出文件大小限制',
                    desc: '文件 ' + file.name + ' 太大，不能超过 2M。'
                });
            },
            handleBeforeUpload() {
                const check = this.imageList.length < 2;
                if (!check) {
                    this.$Notice.warning({
                        title: '最多只能上传6张图片。'
                    });
                }
                return check;
            }
        }
    }
</script>